package zuk.clipvietfun.likeandhistory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;

import zuk.clipvietfun.R;
import zuk.clipvietfun.clip.Clip;
import zuk.clipvietfun.plus.DirAndFile;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class History {
	Context context;
	public History(FragmentActivity activity) {
		this.context = (Context) activity;
	}

	public History(Context context) {
		this.context = context;
	}

	public void addHistory(Clip video,boolean falseIsDef) {
		try {
			if(isHas(video))
				return;
			JSONArray jSonHistory;
			if(!falseIsDef){
				jSonHistory = loadHistory();
			}else{
				jSonHistory = new JSONArray();
			}
			JSONObject hItem = new JSONObject();
			hItem.put("Url", video.getId());
			hItem.put("ThumbImg", video.getImgurl());
			hItem.put("Tittle", video.getTitle());
			hItem.put("UTube", video.getuTube());
			jSonHistory.put(hItem);
			saveHistory(jSonHistory);
			
		} catch (Exception e) {
			addHistory(video,true);
		}
	}

	public void removeHistory(Clip video) {
		try {
			JSONArray jSonHistory = loadHistory();
			JSONArray jSonHistoryRemoved = new JSONArray();
			for(int i=0;i<jSonHistory.length();i++){
				JSONObject hItem = jSonHistory.getJSONObject(i);
				if(!hItem.getString("UTube").equals(video.getuTube())){
					jSonHistoryRemoved.put(hItem);
				}else {
				}
			}
			Toast.makeText(context, R.string.removehistory, Toast.LENGTH_SHORT).show();
			saveHistory(jSonHistoryRemoved);
		} catch (Exception e) {
		}
	}


	public boolean isHas(Clip video) {
		try {
			JSONArray jSonHistory = loadHistory();
			for(int i = 0; i < jSonHistory.length();i++){
				JSONObject hItem = jSonHistory.getJSONObject(i);

				if(hItem.getString("UTube").equals(video.getuTube())){
					return true;
				}
			}
			return false;
		} catch (Exception e) {

		}
		return false;
	}

	public boolean saveHistory(JSONArray jSonHistory) {
		try {
			File dirHistory = new DirAndFile().makeDirHistory(context);
			File fileHistory = new DirAndFile().makeFileHistory(dirHistory);
			OutputStream fOut = new FileOutputStream(fileHistory);
			byte[] bData = jSonHistory.toString().getBytes();
			fOut.write(bData);
			fOut.flush();
			fOut.close();
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	public JSONArray loadHistory() {
		try {
			File dirHistory = new DirAndFile().makeDirHistory(context);
			File fileHistory = new DirAndFile().makeFileHistory(dirHistory);
			FileInputStream fis = new FileInputStream(fileHistory);
			String data = new Scanner(fis,"UTF-8").useDelimiter("\\A").next();
			JSONArray jSonHistory = new JSONArray(data);
			return jSonHistory;
		} catch (Exception e) {
		}
		return null;
	}
}
